package com.tpfinal.agenda.view.renderers;

import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.tpfinal.agenda.model.Contact;

public class ContactsTableModel extends DefaultTableModel {

	private static final long serialVersionUID = 1L;

	static final int COLUMN_IDX_NOMBRE_RAZON = 0;
	static final int COLUMN_IDX_TIPO = 1;
	static final int COLUMN_IDX_DIRECCION = 2;
	static final int COLUMN_IDX_CP = 3;

	static final int COLUMN_IDX_CITY = 4;
	static final int COLUMN_IDX_STATE = 5;
	static final int COLUMN_IDX_DDN = 6;

	static final int COLUMN_IDX_TELEFONO = 7;
	static final int COLUMN_IDX_EMAIL = 8;
	static final int COLUMN_IDX_TELEFONO_MOVIL = 9;
	static final int COLUMN_IDX_FECHA_NACIMIENTO = 10;
	static final int COLUMN_IDX_WEB = 11;
	static final int COLUMN_IDX_RUBROS = 12;

	static final String COLUMN_NOMBRE_RAZON = "Nombre/Razon Social";
	static final String COLUMN_TIPO = "Tipo";
	static final String COLUMN_DIRECCION = "Direccion";
	static final String COLUMN_CP = "Código Postal";

	static final String COLUMN_CITY = "Ciudad";
	static final String COLUMN_STATE = "Provincia";
	static final String COLUMN_DDN = "DDN";

	static final String COLUMN_TELEFONO = "Teléfono";
	static final String COLUMN_EMAIL = "E-mail";
	static final String COLUMN_TELEFONO_MOVIL = "Telefono Movil";
	static final String COLUMN_FECHA_NACIMIENTO = "Fecha de Nacimiento";
	static final String COLUMN_WEB = "Web Site";
	static final String COLUMN_RUBROS = "Rubros";

	private ArrayList<ContactViewAdapter> contactsListView;

	private JTable parent;
	private Filter filter;

	public ContactsTableModel(JTable parent) {

		this.parent = parent;
		this.filter = new FilterAll(this);

		this.addColumn(COLUMN_NOMBRE_RAZON);
		this.addColumn(COLUMN_TIPO);
		this.addColumn(COLUMN_DIRECCION);
		this.addColumn(COLUMN_CP);
		this.addColumn(COLUMN_CITY);
		this.addColumn(COLUMN_STATE);
		this.addColumn(COLUMN_DDN);
		this.addColumn(COLUMN_TELEFONO);
		this.addColumn(COLUMN_EMAIL);
		this.addColumn(COLUMN_TELEFONO_MOVIL);
		this.addColumn(COLUMN_FECHA_NACIMIENTO);
		this.addColumn(COLUMN_WEB);
		this.addColumn(COLUMN_RUBROS);

		contactsListView = new ArrayList<ContactViewAdapter>();

	}

	public JTable getParent() {
		return this.parent;
	}

	public void addContacts(ArrayList<Contact> list) {
		this.contactsListView.clear();
		for (Contact c : list) {
			addContact(c);
		}
	}

	public void addContact(Contact contact) {
		this.contactsListView.add(new ContactViewAdapter(contact));
	}

	ArrayList<ContactViewAdapter> getContactsListView() {
		return this.contactsListView;
	}

	void refresh() {
		this.clear();
		for (ContactViewAdapter viewAdapter : this.contactsListView) {
			if (viewAdapter.isVisible()) {
				this.addRow(viewAdapter.getForTable());
			}
		}
	}

	public void clear() {
		int numrows = this.getRowCount();
		for (int i = numrows - 1; i >= 0; i--) {
			this.removeRow(i);
		}
	}

	public void renderJustPersons() {
		filter = new FilterPersons(this);
		render();
	}

	public void renderJustCompanies() {
		filter = new FilterCompanies(this);
		render();
	}

	public void renderAll() {
		filter = new FilterAll(this);
		render();
	}

	public void render() {
		filter.render();
	}

	public void render(String stringFilter) {
		filter.render(stringFilter);
	}

	public Contact getSelectedContact() {
		int i = parent.getSelectedRow();
		Object cell = parent.getValueAt(i, 0);
		if (cell != null && cell instanceof ContactViewAdapter.Cell) {
			return ((ContactViewAdapter.Cell) cell).getParent().getAdaptee();
		}
		return null;

	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}
}
